var Macro = require('Macro');

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var self = this;

        var call = function(data) {
            self.startHandler(data);
        };
        ClientHandler.add(Macro.game.start, call);

        var call = function(data) {
            self.selectHandler(data);
        };
        ClientHandler.add(Macro.game.select, call);

        var call = function(data) {
            self.surrenderHandler(data);
        };
        ClientHandler.add(Macro.game.surrender, call);

        var call = function(data) {
            self.endHandler(data);
        };
        ClientHandler.add(Macro.game.end, call);

        var call = function(data) {
            self.overHandler(data);
        };
        ClientHandler.add(Macro.game.over, call);

        var call = function(data) {
            self.userStatusHandler(data);
        };
        ClientHandler.add(Macro.user.status, call);
    },

    // start () {

    // },

    // update (dt) {},
    onDestroy() {
        ClientHandler.del(Macro.game.start);
        ClientHandler.del(Macro.game.select);
        ClientHandler.del(Macro.game.surrender);
        ClientHandler.del(Macro.game.end);
        ClientHandler.del(Macro.game.over);
        ClientHandler.del(Macro.user.status);
    },

    userStatusHandler(data) {
        cc.log('Update user status.', data);
        var simulator = this.node.getComponent("GameSimulator");
        simulator.p.players[data.cid].status = data.status;
    },

    startHandler(data) {
        cc.log('Game is started.', data);
        var pl = this.node.getChildByName("Panel_16"); 
        pl.active = true;

        var simulator = this.node.getComponent("GameSimulator");
        var cards = simulator.p.players[0].cards;
        cc.log(simulator.p.players)
        var plCards = this.node.getChildByName("Panel_16").getChildByName("panel_cards");
        for (var i = 0, len = cards.data.length; i < len; i++)
        {
            var cdata = cards.data[i];
            var tmpcards = cards;
            if (cdata.type != 4) {
                cc.loader.loadRes("prefabs/card", function (err, prefab) {
                    // cc.log(cdata);
                    var newNode = cc.instantiate(prefab);
                    plCards.addChild(newNode);
                    tmpcards.nodes.push(newNode);

                    var color1 = newNode.getChildByName("06a0").getComponent(cc.Sprite);
                    var point = newNode.getChildByName("06a010").getComponent(cc.Sprite);
                    var color2 = newNode.getChildByName("06a01").getComponent(cc.Sprite);
                    var pointImgName = "";
                });
            }
            else {
                cc.loader.loadRes("prefabs/cardKing", function (err, prefab) {
                    // cc.log(cdata);
                    var newNode = cc.instantiate(prefab);
                    plCards.addChild(newNode);

                    var color1 = newNode.getChildByName("06a0").getComponent(cc.Sprite);
                    var point = newNode.getChildByName("06a010").getComponent(cc.Sprite);
                    var color2 = newNode.getChildByName("06a01").getComponent(cc.Sprite);
                    var pointImgName = "";
                    cc.log('color1', color1);
                });
            }
        }

        for (var i = 0, len = cards.nodes.length; i < len; i++)
        {
            var nod = cards.nodes[i];
            nod.x = i * 30;
            cc.log("i", i)
        }
    },

    selectHandler(data) {
        cc.log('select.', data);
    },

    surrenderHandler(data) {
        cc.log('surrender.', data);
    },

    endHandler(data) {
        cc.log('Game is end.', data);
    },

    overHandler(data) {
        cc.log('Game is over.', data);
    },

    quitGame() {
        cc.director.loadScene("LobbyScene");
    },

});
